package com.chookss.mine.personal.collection;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.chookss.R;
import com.chookss.mine.MyOrderPagerAdapter;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.NavigationTools;
import com.chookss.tools.PermissionSpUtils;
import com.chookss.tools.Utils;
import com.chookss.view.ColorFlipPagerTitleView;
import com.johnrambo.ktea.common.klogutil.KLog;
import com.lvgroup.hospital.base.BaseAct;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyCollectionActivity extends BaseAct {


    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.common_right_txt)
    TextView commonRightTxt;
    @BindView(R.id.indicator_view)
    MagicIndicator indicatorView;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.flBottom)
    RelativeLayout flBottom;
    @BindView(R.id.tvBottom)
    TextView tvBottom;
    private List<String> titles = new ArrayList<>();
    private MyCollectionOrderPagerAdapter pagerAdapter;
    private ArrayList<CollectionBaseFragment> fragments = new ArrayList<>();
    private int deleteNum=0;//删除选择的数量
    private int index=0;//选中的哪个
    private boolean isEdit=false;//是否编辑模式

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        setContentView(R.layout.activity_my_collection);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();

        commonRightTxt.setVisibility(View.VISIBLE);
        commonTitleTxt.setText("收藏");
        commonRightTxt.setText("管理");
        if (PermissionSpUtils.getBoolean(this,"14",false)) {
            titles.add("视频");
            fragments.add(new CollectionVideoFragment("2"));
        }
        if (PermissionSpUtils.getBoolean(this,"19",false)) {
            titles.add("小视频");
            fragments.add(new CollectionVideoFragment("1"));
        }
        if (PermissionSpUtils.getBoolean(this,"03",false)) {
            titles.add("资料");
            fragments.add(new CollectionDocumentFragment());
        }
        if (PermissionSpUtils.getBoolean(this,"13",false)) {
            titles.add("题目");
            fragments.add(new CollectionSubjectFragment());
        }
        if (PermissionSpUtils.getBoolean(this,"26",false)) {
            titles.add("图片");
            fragments.add(new CollectionPicFragment());
        }
        if (PermissionSpUtils.getBoolean(this,"17",false)) {
            titles.add("图文");
            fragments.add(new CollectionTuwenFragment());
        }
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setScrollPivotX(0.65f);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles == null ? 0 : titles.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorFlipPagerTitleView(context);
                simplePagerTitleView.setIncludeFontPadding(false);
                simplePagerTitleView.setText(titles.get(index));
                simplePagerTitleView.setTextSize(15);
                simplePagerTitleView.setPadding(Utils.dip2px(MyCollectionActivity.this, 7), 0, Utils.dip2px(MyCollectionActivity.this, 7), 0);
                simplePagerTitleView.setNormalColor(Color.rgb(153, 153, 153));
                simplePagerTitleView.setSelectedColor(Color.rgb(225, 36, 22));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(UIUtil.dip2px(context, 2));
                indicator.setLineWidth(UIUtil.dip2px(context, 46));
                indicator.setRoundRadius(UIUtil.dip2px(context, 1));
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                indicator.setColors(Color.rgb(225, 36, 22));
                return indicator;
            }
        });
        indicatorView.setNavigator(commonNavigator);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                indicatorView.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                indicatorView.onPageSelected(position);
                index=position;
                if(fragments.get(position).getSize()>0&&isEdit){
                    flBottom.setVisibility(View.VISIBLE);
                }else{
                    flBottom.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                indicatorView.onPageScrollStateChanged(state);
            }
        });
        pagerAdapter = new MyCollectionOrderPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(5);
        NavigationTools.setWaterMark(this, "20");
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MyEvent messageEvent) {
        //事件订阅者处理事件
        if (messageEvent.getMap() != null) {
            Map<String, String> map = messageEvent.getMap();
        } else {
            String msg = messageEvent.getMessage();
            if (Utils.isNull(msg)) {
                return;
            }
            if (msg.equals("Collection_Add")) {
                deleteNum++;
                tvBottom.setText("已选中"+deleteNum+"个");
            } else if (msg.equals("Collection_Subtract")) {
                deleteNum--;
                if(deleteNum<=0){
                    tvBottom.setText("已选中0个");
                }else{
                    tvBottom.setText("已选中"+deleteNum+"个");
                }
            }else if (msg.equals("refresh_collection")) {
                if(fragments.get(index).getSize()>0&&isEdit){
                    flBottom.setVisibility(View.VISIBLE);
                }else{
                    flBottom.setVisibility(View.GONE);
                }
            }
        }
    }
    @OnClick({R.id.common_title_back, R.id.common_right_txt, R.id.tvDelete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.common_right_txt:
                if(commonRightTxt.getText().toString().equals("完成")){
                    flBottom.setVisibility(View.GONE);
                    commonRightTxt.setText("管理");
                    isEdit=false;
                    EventBus.getDefault().post(new MyEvent("Collection_Save"));
                }else{
                    isEdit=true;
                    flBottom.setVisibility(View.VISIBLE);
                    tvBottom.setText("已选中"+deleteNum+"个");
                    commonRightTxt.setText("完成");
                    EventBus.getDefault().post(new MyEvent("Collection_Edit"));
                }
                break;
            case R.id.tvDelete:
                if(deleteNum==0){
                    MyToast.show("至少选择一个");
                    return;
                }
                EventBus.getDefault().post(new MyEvent("Collection_Delete"));
                deleteNum=0;
                tvBottom.setText("已选中"+deleteNum+"个");
                break;
        }
    }
}
